package com.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.VO.DemandAcceptDetailVO;
import com.web.annotation.CurrentUser;
import com.web.domain.Response;
import com.web.VO.UnReadNoticeVO;
import com.web.entity.Notice;
import com.web.entity.UserInfo;
import com.web.service.DemandService;
import com.web.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/app/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private DemandService demandService;

    /**
     * 消息查询
     * @param userInfo
     * @param type
     * @param pageNum
     * @return
     */
    @RequestMapping("/searchNotice")
    public Response searchNotice(@CurrentUser UserInfo userInfo,
                                 Integer type, Integer pageNum) {
        if (pageNum == null) {
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        PageHelper.startPage(pageNum,10);
        List<Notice> noticeList = noticeService.listNotice(type, userInfo.getUserId());
        PageInfo pageInfo = new PageInfo(noticeList,10);
        if(pageNum > pageInfo.getPages()){
            return Response.custom().ok().addParams(new List[0]);
        }else{
            return Response.custom().ok().addParams(pageInfo.getList());
        }
    }

    /**
     * 未读消息数
     * @return
     */
    @RequestMapping("/unReadNotice")
    public Response unReadNoticeNum(@CurrentUser UserInfo info) {
        UnReadNoticeVO unReadNoticeVO = noticeService.getUnReadNoticeNum(info);
        return Response.custom().ok().addParams(unReadNoticeVO);
    }

    /**
     * 接单详情
     * @param demandId 需求id
     * @param userId 接单人id
     * @return
     */
    @RequestMapping("/demandAcceptDetail")
    public Response demandAcceptDetail(Integer demandId, String userId) {
        DemandAcceptDetailVO detailVO = demandService.getDemandDetail(demandId, userId);
        return Response.custom().ok().addParams(detailVO);
    }
}
